﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp1.bean;
using WpfApp1.Httpwork;
using WpfApp1.Unit;
/**确认组盘上传窗口*/
namespace WpfApp1
{
    /// <summary>
    /// zupalletdialog.xaml 的交互逻辑
    /// </summary>
    public partial class zupalletdialog : Window,HttpPostZupanInterface
    {
        bool isqueding = false;
        string pan;
        public zupalletdialog(string pan)
        {
            this.pan = pan;
            InitializeComponent();
            ArrayList gaodu = new ArrayList();
            gaodu.Add("600mm");
            gaodu.Add("800mm");
            gaoduCB.ItemsSource = gaodu;
            gaoduCB.Text = "800mm";
            position_buffer.Text = pan;
        }

        private void queding(object sender, RoutedEventArgs e)
        {
            if (isqueding)
            {
                return;
            }
            isqueding = true;

            ArrayList pcodes = new ArrayList();
            
            zupan bean = new zupan();
            string content = "";
            bean.position_buffer = position_buffer.Text;
            bean.height = gaoduCB.Text;
            bean.pallet_num = Window1.panonum[pan].pallet;
            
            
            bean.confirm_code = 0;
            foreach (KeyValuePair<String, packagebean> p in Window1.packages)
            {
                var package = p.Value;
                if (package.pallet_id == pan)
                {
                    pcodes.Add(package.pcode);
                }
            }
            bean.pcodes = pcodes;
            content = JsonConvert.SerializeObject(bean);
            Console.WriteLine(content);
            Http.PostZupan(this, content,pan);
        }

        private void quxiao(object sender, RoutedEventArgs e)
        {
            Window window = Window.GetWindow(this);//关闭父窗体
            window.Close();
        } 

        private void GaoduCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public void httpPostZupanOk(string data,String pan)
        {
            isqueding = false;
            JObject obj = JObject.Parse(data);
            string status = (String)obj["status"];
            string s = (String)obj["msg"];
            if (status == "success")
            {
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    DialogResult = true;
                }));
                
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    DialogResult = true;
                    //Window window = Window.GetWindow(this);//关闭父窗体
                    //window.Close();
                    //MessageBox.Show(s);
                }));
                
                
            }
        }

        public void httpPostZupanNo(string e, bool b)
        {
            isqueding = false;
            Dispatcher.BeginInvoke(new Action(delegate
            {
                System.Windows.Forms.MessageBox.Show(e);
                Window window = Window.GetWindow(this);//关闭父窗体
                window.Close();
            }));
            
        }
    }
    class zupan
    {
        public ArrayList pcodes { get; set; }
        public String pallet_num { get; set; }
        public String height { get; set; }
        public String position_buffer { get; set; }
        public int confirm_code { get; set; }
    }
}
